package com.jhf.skypac_pda.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.CustomFooterViewCallBack;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.jhf.skypac_pda.EventMessage.MessageEvent;
import com.jhf.skypac_pda.LoginActivity;
import com.jhf.skypac_pda.MainActivity;
import com.jhf.skypac_pda.R;
import com.jhf.skypac_pda.adapter.ExceptionListAdapter;
import com.jhf.skypac_pda.base.BaseFragment;
import com.jhf.skypac_pda.model.DialogCallback;
import com.jhf.skypac_pda.model.ExceptionListModel;
import com.jhf.skypac_pda.model.LzyResponse2;
import com.jhf.skypac_pda.utils.PrefUtils;
import com.jhf.skypac_pda.utils.Urls;
import com.jhf.skypac_pda.view.CustomLoadingMoreFooter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import okgo.OkGo;
import okgo.model.HttpHeaders;
import okgo.model.Response;
import okhttp3.RequestBody;

import static com.jhf.skypac_pda.EventMessage.MessageEvent.UPDATE_EXCEPTION_DONE_VIEW;
import static com.jhf.skypac_pda.utils.ConstantPara.JSON;

public class ExceptionDoneFragment extends BaseFragment implements XRecyclerView.LoadingListener{
    @BindView(R.id.rvData)
    XRecyclerView rvData;
    @BindView(R.id.ll_empty)
    LinearLayout imgEmpty;

    private List<ExceptionListModel> mList = new ArrayList<>();
    private ExceptionListAdapter mAdapter;
    private boolean isFirst = true;
    private int pageNum = 1;
    private String orderType = "1";

    public static ExceptionDoneFragment newInstance(String orderType, String search) {
        ExceptionDoneFragment fragment = new ExceptionDoneFragment();
        Bundle bundle = new Bundle();
        bundle.putString("orderType", orderType);
        bundle.putString("search", search);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onRefresh() {
        pageNum = 1;
        getData();
    }

    @Override
    public void onLoadMore() {
        getData();
    }

    @Override
    public void onResume() {
        super.onResume();
        orderType = getArguments().getString("orderType");
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rvData.setLayoutManager(manager);
        mAdapter = new ExceptionListAdapter(getActivity(), mList, orderType);
        rvData.setAdapter(mAdapter);
        final CustomLoadingMoreFooter footer = new CustomLoadingMoreFooter(getActivity());
        rvData.setFootView(footer, new CustomFooterViewCallBack() {
            @Override
            public void onLoadingMore(View yourFooterView) {
                footer.onLoadingMore();
            }

            @Override
            public void onLoadMoreComplete(View yourFooterView) {
                footer.onLoadMoreComplete();
            }

            @Override
            public void onSetNoMore(View yourFooterView, boolean noMore) {
                footer.onSetNoMore();
            }
        });
        rvData.setPullRefreshEnabled(true);
        rvData.setLoadingMoreEnabled(true);
        rvData.setLoadingListener(this);

        if (isFirst) {
            isFirst = false;
            refreshData();
        }
    }

    private void refreshData() {
        pageNum = 1;
        getData();
    }

    private void getData() {
        Map<String, Object> map = new HashMap<>();
        map.put("pageNum", 1);
        map.put("pageSize", 10);

        Gson gg = new Gson();
        String json = gg.toJson(map);

        RequestBody body = RequestBody.create(JSON, json);

        HttpHeaders headers = new HttpHeaders();
        headers.put("Authorization", PrefUtils.getString(getActivity(), "token", ""));
        OkGo.<LzyResponse2<List<ExceptionListModel>>>get(Urls.URL_GET_TASK_EXCEPTION_RECORD + "?pageNum=" + pageNum + "&pageSize=" + 10 + "&exceptionTaskStatus=" + orderType)
                .tag(this)
                .headers(headers)
                .execute(new DialogCallback<LzyResponse2<List<ExceptionListModel>>>(getActivity()) {
                    @Override
                    public void onSuccess(Response<LzyResponse2<List<ExceptionListModel>>> response) {
                        if (null != response.body().rows) {
                            if (pageNum == 1) {
                                mList.clear();
                            }

                            mList.addAll(response.body().rows);

                            if (null == mList || mList.size() == 0) {
                                imgEmpty.setVisibility(View.VISIBLE);
                            } else {
                                imgEmpty.setVisibility(View.GONE);
                            }

                            if (null == response.body().rows || response.body().rows.size() == 0) {
                                rvData.refreshComplete();
                                rvData.loadMoreComplete();
                                rvData.setNoMore(true);

                                return;
                            } else {
                                if (null != response.body().rows && response.body().rows.size() > 0) {
                                    rvData.refreshComplete();
                                    pageNum++;

                                    mAdapter.notifyDataSetChanged();
                                }

                                if (response.body().total == mList.size()) {
                                    rvData.refreshComplete();
                                    rvData.loadMoreComplete();
                                    rvData.setNoMore(true);
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(Response<LzyResponse2<List<ExceptionListModel>>> response) {
                        Toast.makeText(getActivity(), response.getException().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
    }

    @Override
    protected void setUpContentView() {
        setContentView(R.layout.fragment_exception_todo);
    }

    @Override
    protected void setUpView(View view) {

    }

    @Override
    protected void setUpComponent() {
    }

    @Override
    protected void setUpData() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onShutDown(MessageEvent eventMsg) {
        String msg = eventMsg.getMessage();
        if (eventMsg.getType() == UPDATE_EXCEPTION_DONE_VIEW) {
            refreshData();
        }
    }
}
